package com.infomanage.service.worklog;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.AttendanceDAO;
import com.infomanage.dao.DutylogDAO;
import com.infomanage.dao.PersonDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.Dutylog;
import com.infomanage.po.Person;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.infomanage.vo.DutylogItem;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

public class DutylogService {

	public static DutylogItem loadDutylog(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			String dutyId= request.getParameter("dutyId");
			if(StringUtils.isEmpty(dutyId)) return null;
			DutylogDAO dao = new DutylogDAO();
			return dao.find(Integer.parseInt(dutyId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<DutylogItem> loadDutylogList(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			DutylogDAO dao = new DutylogDAO();
			List<DutylogItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ReturnObject saveDutylogInfo(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			Dutylog dutylog = new Dutylog();
			dutylog.setBegintime(request.getParameter("begintime"));
			dutylog.setDescription(request.getParameter("description"));
			dutylog.setEndtime(request.getParameter("endtime"));
			dutylog.setImportWork(request.getParameter("importwork"));
			dutylog.setNotify(request.getParameter("notify"));
			dutylog.setPersonId(Integer.parseInt(request.getParameter("personId")));
			dutylog.setProblem(request.getParameter("problem"));
			dutylog.setTodo(request.getParameter("todo"));
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.find(Person.class, Integer.parseInt(request.getParameter("personId")));
			if(person != null){
				dutylog.setDeptId(person.getDeptId());
			}
			DutylogDAO dao = new DutylogDAO();
			dao.add(dutylog);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
			e.printStackTrace();
		}
		return ret;
	}
	
	public static ReturnObject modifyDutylogInfo(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int planId = Integer.parseInt(request.getParameter("dutyId"));
			DutylogDAO dao = new DutylogDAO();
			Dutylog dutylog = dao.find(Dutylog.class, planId);
			dutylog.setBegintime(request.getParameter("begintime"));
			dutylog.setDescription(request.getParameter("description"));
			dutylog.setEndtime(request.getParameter("endtime"));
			dutylog.setImportWork(request.getParameter("importwork"));
			dutylog.setNotify(request.getParameter("notify"));
			dutylog.setPersonId(Integer.parseInt(request.getParameter("personId")));
			dutylog.setProblem(request.getParameter("problem"));
			dutylog.setTodo(request.getParameter("todo"));
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.find(Person.class, Integer.parseInt(request.getParameter("personId")));
			if(person != null){
				dutylog.setDeptId(person.getDeptId());
			}
			dao.modify(dutylog);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static ReturnObject removeDutylogInfo(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String dutylogId = request.getParameter("dutyId");
			DutylogDAO dao = new DutylogDAO();
			dao.remove(Dutylog.class, Integer.parseInt(dutylogId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
			e.printStackTrace();
		}
		return ret;
	}
	
	public static ReturnObject removeBatchDutylogInfo(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			DutylogDAO dao = new DutylogDAO();
			if(batchIds != null){
				String[] ids = batchIds.split(";");
				for(String id : ids){
					dao.remove(Dutylog.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static List<DutylogItem> search(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			DutylogDAO dao = new DutylogDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			//设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if(currPageNum == null){
				page.setCurrentPageNum(1);
			}else{
				page.setCurrentPageNum(Integer.parseInt(currPageNum));
				
			}
			searchBean.setPage(page);
			List<DutylogItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			DutylogDAO dao = new DutylogDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1)/PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}
	
	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String personId = request.getParameter("personId");
		String beginDay = request.getParameter("beginDay");
		String endDay = request.getParameter("endDay");
		SearchBean searchBean = new SearchBean();
		if(!StringUtil.isEmpty(personId)){
			FieldBean field = new FieldBean("personId", Integer.parseInt(personId));
			searchBean.add(field);
		}
		if (!StringUtil.isEmpty(beginDay)) {
			searchBean.setBeginTimeName("begintime");
			searchBean.setBeginTimeValue(beginDay);
		}
		if (!StringUtil.isEmpty(endDay)) {
			searchBean.setEndTimeName("endtime");
			searchBean.setEndTimeValue(endDay);
		}
		//设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if(user != null){
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
		return searchBean;
	}
}
